package cn.tedu.jdbc;

import java.sql.*;

//删除所有zhaoxin
public class JDBCTest03 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try {
            //1.注册驱动
            //不常用
            /*Driver driver = new com.mysql.cj.jdbc.Driver();
            DriverManager.registerDriver(driver);*/
            //常用
            Class.forName("com.mysql.cj.jdbc.Driver");//需要抛出ClassNotFoundException

            //2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "root");

            //3.获取数据库操作对象
            stmt = conn.createStatement();

            //4.执行sql语句"删除"
            String sql = "delete from t_user " +
                    "where name = 'zhaoxin'";
            int count = stmt.executeUpdate(sql);
            System.out.println("删除了" + count + "条数据");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            //6.释放资源
            if (stmt != null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
